package com.jdbc.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.joke.util.Const;
import com.joke.util.DateUtil;
import com.joke.util.StringUtil;


public class MysqlServerConnect {
	private static final Logger logger = LoggerFactory.getLogger(MysqlServerConnect.class);

	private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	private static final String DB_URL = "jdbc:mysql://localhost:9654/joke";
	private static final String USER = "joke";
	private static final String PASS = "1QIHXlyrLRrmDKq0";
    
	private static Connection conn = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;
	private static PreparedStatement prestmt = null;
    
    public static Connection getConnect() {
        try {
        	// 注册 JDBC 驱动
			Class.forName(JDBC_DRIVER);
			// 打开链接
			conn = DriverManager.getConnection(DB_URL,USER,PASS);
			// 执行查询
			stmt = conn.createStatement();
//			logger.info("连接server数据库成功");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return conn;
    }
	private static void close() {
		try {
			if(rs!=null) rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try{
		    if(stmt!=null) stmt.close();
		}catch(SQLException se2){
		} 
		
		try{
		    if(prestmt!=null) prestmt.close();
		}catch(SQLException se2){
		} 
		
		try{
		    if(conn!=null) conn.close();
		}catch(SQLException se){
		    se.printStackTrace();
		}
	}
    public static Boolean confirmConnect() {
        try {
        	// 注册 JDBC 驱动
			Class.forName(JDBC_DRIVER);
			// 打开链接
			conn = DriverManager.getConnection(DB_URL,USER,PASS);
			// 执行查询
			stmt = conn.createStatement();
		} catch (ClassNotFoundException e) {
			return false;
		} catch (SQLException e) {
			return false;
		}finally{
			close();
		}
        return true;
    }
    
    /**
     * 保存用户昵称和头像
     * @param map
     */
    public static void insertUser(Map<String,String> map) {
    	getConnect();
        try{
            String sql = " INSERT INTO yi_user " + 
            		" (username,password,email,sex,qq,phone,level,money,avatar,audit_num,audit_suc_num,audit_pro,send_num,register_ip,last_login_ip,last_login_time,created_time,online_time,status,openid,sign_time,follows,fans) " + 
            		" VALUES ( ?, '', '', '2', null, null, '1', '0', ?, '0', '0', '0', '0', '127.0.0.1', '127.0.0.1', '0', '0', '0', '1', null, '0', '0', '0')";
            prestmt = conn.prepareStatement(sql);
            logger.info("保存数据...");
            int count = 0;
			for (String str : map.keySet()) {
				prestmt.setString(1,str);
            	prestmt.setString(2,map.get(str));
				prestmt.addBatch();
				count++;
			}
//			prestmt.executeBatch();
			prestmt.clearBatch();
            logger.info("保存用户数据完成:"+count); 
        }catch(SQLException e){
        	logger.error("insertUser,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertUser,Exception:"+e.getMessage());
        }finally{
        	close();
        }
    
    }

    /**
     * 保存段子（文本）
     * @param map
     */
	public static void insertTxT(Object[] txt) {
    	getConnect();
        try{
             String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES ('段子', '', ?, 0, 0, 0,?, ?, 1, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[txt.length];
    		
    		for (int j = 0; j < txt.length; j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
//    		logger.info("段子ids总长度："+ids.length);
    		
    		Date date = new Date();
    		int count = 0;
    		Map<String, String> map = new HashMap<>();
            for (int i = 0; i < txt.length; i++) {
        		prestmt.setString(1,"&lt;p&gt;"+txt[i].toString().trim()+"&lt;/p&gt;");
        		prestmt.setLong(2, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
        		prestmt.setLong(3, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
        		prestmt.setLong(4, ids[i]);
        		prestmt.setLong(5, new Random().nextInt(950) + 50);
        		prestmt.setLong(6, new Random().nextInt(95) + 5);
        		prestmt.addBatch();
        		count++;
        		map.put(txt[i].toString().trim(), "");
			}
            
			prestmt.executeBatch();
//        	prestmt.clearBatch();
            logger.info("保存<段子>数据到server完成:"+count); 
            MysqlLocalConnect.insert(map, "段子");
        }catch(SQLException e){
        	logger.error("insertTxT,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertTxT,Exception:"+e.getMessage());
        }finally{
            close();
        }
    
    }
    
    /**
     * 保存热门
     * @param map
     */
    public static void insertHot(Map<String,String> map) {
    	String Y = DateUtil.formatString(new Date(), "yyyy");
    	String M = DateUtil.formatString(new Date(), "MM");
    	String D = DateUtil.formatString(new Date(), "dd");
    	String ymd = Y+"/"+M+"/"+D;
    	getConnect();
        try{
            String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES ('热门', ?, ?, 0, 0, 0, ?, ?, ?, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[map.size()];
    		
    		for (int j = 0; j < map.size(); j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		
//    		logger.info("热门ids总长度："+ids.length);
    		int type = 1;
    		int index = 0;
    		Date date = new Date();
    		Map<String, String> loaclmap = new HashMap<>();
			for (String str : map.keySet()) {
				String path = map.get(str).trim();
				String content = str.trim();
				String locContent = content.trim();
				String locPath = path.trim();
				if (!"".equals(path.trim())) {
					path = Const.reSymbol+Const.jokePre+ymd+Const.o + path;
/*					if (path.toLowerCase().indexOf("gif".toLowerCase()) != -1) {
						type = 2;
						content = "&lt;p class=&quot;dp-b&quot; style=&quot;"
								+ "margin: 0px; padding: 0px; border: none;"
								+ " word-break: break-all; font-size: 22px; font-weight: normal;"
								+ " color: rgb(50, 50, 50); font-family: &amp;quot;Microsoft Yahei&amp;"
								+ "quot;, 微软雅黑, Tahoma, Arial, STHeiti, sans-serif;"
								+ " white-space: normal; background-color: rgb(255, 255, 255);&quot;&gt;"
								+content+"&lt;/p&gt;&lt;p&gt;"+"&lt;img src=&quot;"+path
								+"&quot; alt=&quot;&quot; val=&quot;"+path+"&quot;/&gt;"+"&lt;/p&gt;";
					} else {*/
					type = 2;
					content = "&lt;p&gt;" + content + "&lt;br/&gt;" + "&lt;img src=&quot;" + path
							+ "&quot; alt=&quot;&quot; val=&quot;" + path + "&quot;/&gt;" + "&lt;/p&gt;";
					/*}*/
				}else {
					content = "&lt;p&gt;"+content+"&lt;/p&gt;";
					type = 1;
				}
				
				prestmt.setString(1,path.trim());//路径
            	prestmt.setString(2,content.trim());//内容
				prestmt.setLong(3, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
				prestmt.setLong(4, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
            	prestmt.setLong(5,type);//类型
            	prestmt.setLong(6,ids[index]);//用户Id
            	prestmt.setLong(7,new Random().nextInt(1300) + 700);//赞
            	prestmt.setLong(8,new Random().nextInt(30));//踩
				prestmt.addBatch();
				index++;
				loaclmap.put(locContent, locPath);
				
			}
			prestmt.executeBatch();
//			prestmt.clearBatch();
            logger.info("保存<热门>数据到server完成:"+index); 
    		MysqlLocalConnect.insert(loaclmap, "热门");
        }catch(SQLException e){
        	logger.error("insertHot,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertHot,Exception:"+e.getMessage());
        }finally{
            close();
        }
    
    }
    
    /**
     * 保存趣图（文本+图片）
     * @param map
     */
    public static void insertImage(Map<String,String> map) {
    	String Y = DateUtil.formatString(new Date(), "yyyy");
    	String M = DateUtil.formatString(new Date(), "MM");
    	String D = DateUtil.formatString(new Date(), "dd");
    	String ymd = Y+"/"+M+"/"+D;
    	getConnect();
        try{
            String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES ('趣图', ?, ?, 0, 0, 0, ?, ?, ?, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[map.size()];
    		
    		for (int j = 0; j < map.size(); j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		
//    		logger.info("趣图ids总长度："+ids.length);
    		int type = 1;
    		int index = 0;
    		Date date = new Date();
    		Map<String, String> loaclmap = new HashMap<>();
			for (String str : map.keySet()) {
				String path = map.get(str).trim();
				String content = str.trim();
				String locContent = content.trim();
				String locPath = path.trim();
				if (!"".equals(path.trim())) {
					path = Const.reSymbol+Const.jokePre+ymd+Const.o+path;
					type = 2;		
					content = "&lt;p&gt;" + content + "&lt;br/&gt;" + "&lt;img src=&quot;" + path
							+ "&quot; alt=&quot;&quot; val=&quot;" + path + "&quot;/&gt;" + "&lt;/p&gt;";
				}else {
					content = "&lt;p&gt;"+content+"&lt;/p&gt;";
					type = 1;
				}
				
				prestmt.setString(1,path.trim());//路径
            	prestmt.setString(2,content.trim());//内容
				prestmt.setLong(3, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
				prestmt.setLong(4, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
            	prestmt.setLong(5,type);//类型
            	prestmt.setLong(6,ids[index]);//用户Id 
            	prestmt.setLong(7, new Random().nextInt(950) + 50);//赞
            	prestmt.setLong(8, new Random().nextInt(95) + 5);//踩
				prestmt.addBatch();
				index++;
				loaclmap.put(locContent, locPath);
			}
			
			prestmt.executeBatch();
//			prestmt.clearBatch();
            logger.info("保存<趣图>数据到server完成:"+index); 
    		MysqlLocalConnect.insert(loaclmap, "趣图");
        }catch(SQLException e){
        	logger.error("insertImage,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertImage,Exception:"+e.getMessage());
        }finally{
            close();
        }
    
    }
    
    /**
     * 保存视频（文本+视频mp4）
     * @param map
     */
	public static void insertVideo(Map<String, String> map) {
    	getConnect();
        try{
            String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES (?, ?, ?, 0, 0, 0, ?, ?, ?, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[map.size()];
    		
    		for (int j = 0; j < map.size(); j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		
//    		logger.info("视频ids总长度："+ids.length);
    		int type = 1;
    		int index = 0;
    		Date date = new Date();
    		Map<String, String> loaclVodeoMap = new HashMap<>();
    		Map<String, String> loaclVodeoImageMap = new HashMap<>();
			for (String str : map.keySet()) {
				String pathTwo = map.get(str).trim();
				String[] pathTwos = pathTwo.split("\\|\\|");
				if (pathTwos.length<2) continue;
				String videoImagePath = pathTwos[0];
				String videoPath = pathTwos[1];
				String content = str.trim();
				String locContent = content.trim();
				String locVideoPath = videoPath.trim();
				String locImagePath = videoImagePath.trim();
				if (StringUtil.isNotEmpty(videoPath)) {
//					videoImagePath = Const.reSymbol+Const.jokePre+ymd+Const.o+videoImagePath;
//					videoPath = Const.reSymbol+Const.jokePre+ymd+Const.o+videoPath;
					type = 4;	//修改类型和显示格式  将对应属性存到对应字段里
/*					videoImagePath = "&lt;img src=&quot;" + videoImagePath
							+ "&quot; alt=&quot;&quot; val=&quot;" + videoImagePath + "&quot;/&gt;";*/
				
					prestmt.setString(1,content.trim());//标题  是显示内容
					prestmt.setString(2,videoImagePath.trim());//图片路径标签
					prestmt.setString(3,videoPath.trim());//视频URL
					prestmt.setLong(4, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
					prestmt.setLong(5, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
					prestmt.setLong(6,type);//类型
					prestmt.setLong(7,ids[index]);//用户Id 
					prestmt.setLong(8, new Random().nextInt(950) + 50);//赞
					prestmt.setLong(9, new Random().nextInt(95) + 5);//踩
					prestmt.addBatch();
					loaclVodeoMap.put(locContent, locVideoPath);
					loaclVodeoImageMap.put(locContent, locImagePath);
				}
				index++;
			}
			
			prestmt.executeBatch();
//			prestmt.clearBatch();
            logger.info("保存<视频>数据到server完成:"+index); 
    		MysqlLocalConnect.insert(loaclVodeoMap, "视频");
    		MysqlLocalConnect.insert(loaclVodeoImageMap, "视频封面");
        }catch(SQLException e){
        	logger.error("insertImage,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertImage,Exception:"+e.getMessage());
        }finally{
            close();
        }
    
    } 
	/**-----------------------以下的插入方法为火车头采集使用-----------------------------------------**/
	   /**
     * 保存用户昵称和头像
     * @param map
     */
    public static void insertUserLoco(Map<String,String> map) {
    	getConnect();
        try{
            String sql = " INSERT INTO yi_user " + 
            		" (username,password,email,sex,qq,phone,level,money,avatar,audit_num,audit_suc_num,audit_pro,send_num,register_ip,last_login_ip,last_login_time,created_time,online_time,status,openid,sign_time,follows,fans) " + 
            		" VALUES ( ?, '', '', '2', null, null, '1', '0', ?, '0', '0', '0', '0', '127.0.0.1', '127.0.0.1', '0', '0', '0', '1', null, '0', '0', '0')";
            prestmt = conn.prepareStatement(sql);
            logger.info("保存数据...");
			for (String str : map.keySet()) {
				prestmt.setString(1,str);
            	prestmt.setString(2,map.get(str));
				prestmt.addBatch();
			}
			prestmt.executeBatch();
            logger.info("保存数据完成"); 
        }catch(SQLException e){
        	logger.error("insertUser,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertUser,Exception:"+e.getMessage());
        }finally{
        	close();
        }
    
    }

    /**
     * 保存段子（文本）
     * @param map
     */
	public static void insertTxTLoco(Object[] txt) {
    	getConnect();
    	MysqlLocalConnect.getConnect();
        try{
             String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES ('段子', '', ?, 0, 0, 0,?, ?, 1, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[txt.length];
    		
    		for (int j = 0; j < txt.length; j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		logger.info("段子ids总长度："+ids.length);
    		
    		Date date = new Date();
    		int count = 0;
    		Map<String, String> map = new HashMap<>();
            for (int i = 0; i < txt.length; i++) {
            	if(MysqlLocalConnect.query(txt[i].toString().trim(), "")) {
            		prestmt.setString(1,"&lt;p&gt;"+txt[i].toString().trim()+"&lt;/p&gt;");
            		prestmt.setLong(2, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
            		prestmt.setLong(3, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
            		prestmt.setLong(4, ids[i]);
            		prestmt.setLong(5, new Random().nextInt(950) + 50);
            		prestmt.setLong(6, new Random().nextInt(95) + 5);
            		prestmt.addBatch();
            		count++;
            		map.put(txt[i].toString().trim(), "");
            	}
			}
            
	//		prestmt.executeBatch();
        	prestmt.clearBatch();
            logger.info("保存数据serve"+"段子"+"完成:"+count); 
            MysqlLocalConnect.insert(map, "段子");
        }catch(SQLException e){
        	logger.error("insertTxT,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertTxT,Exception:"+e.getMessage());
        }finally{
            close();
            MysqlLocalConnect.close();
        }
    
    }
    
    /**
     * 保存热门
     * @param map
     */
    public static void insertHotLoco(Map<String,String> map) {
    	String Y = DateUtil.formatString(new Date(), "yyyy");
    	String M = DateUtil.formatString(new Date(), "MM");
    	String D = DateUtil.formatString(new Date(), "dd");
    	String ymd = Y+"/"+M+"/"+D;
    	getConnect();
    	MysqlLocalConnect.getConnect();
        try{
            String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES ('热门', ?, ?, 0, 0, 0, ?, ?, ?, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[map.size()];
    		
    		for (int j = 0; j < map.size(); j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		
    		logger.info("热门ids总长度："+ids.length);
    		int type = 1;
    		int index = 0;
    		Date date = new Date();
    		Map<String, String> loaclmap = new HashMap<>();
			for (String str : map.keySet()) {
				String path = map.get(str).trim();
				String content = str.trim();
				if(MysqlLocalConnect.query(content, path)) {
					String locContent = content.trim();
					String locPath = path.trim();
					if (!"".equals(path.trim())) {
						path = Const.reSymbol+Const.jokePre+ymd+Const.o + path;
	/*					if (path.toLowerCase().indexOf("gif".toLowerCase()) != -1) {
							type = 2;
							content = "&lt;p class=&quot;dp-b&quot; style=&quot;"
									+ "margin: 0px; padding: 0px; border: none;"
									+ " word-break: break-all; font-size: 22px; font-weight: normal;"
									+ " color: rgb(50, 50, 50); font-family: &amp;quot;Microsoft Yahei&amp;"
									+ "quot;, 微软雅黑, Tahoma, Arial, STHeiti, sans-serif;"
									+ " white-space: normal; background-color: rgb(255, 255, 255);&quot;&gt;"
									+content+"&lt;/p&gt;&lt;p&gt;"+"&lt;img src=&quot;"+path
									+"&quot; alt=&quot;&quot; val=&quot;"+path+"&quot;/&gt;"+"&lt;/p&gt;";
						} else {*/
						type = 2;
						content = "&lt;p&gt;" + content + "&lt;br/&gt;" + "&lt;img src=&quot;" + path
								+ "&quot; alt=&quot;&quot; val=&quot;" + path + "&quot;/&gt;" + "&lt;/p&gt;";
						/*}*/
					}else {
						content = "&lt;p&gt;"+content+"&lt;/p&gt;";
						type = 1;
					}
					
					prestmt.setString(1,path.trim());//路径
	            	prestmt.setString(2,content.trim());//内容
					prestmt.setLong(3, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
					prestmt.setLong(4, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
	            	prestmt.setLong(5,type);//类型
	            	prestmt.setLong(6,ids[index]);//用户Id
	            	prestmt.setLong(7,new Random().nextInt(1300) + 700);//赞
	            	prestmt.setLong(8,new Random().nextInt(30));//踩
					prestmt.addBatch();
					index++;
					loaclmap.put(locContent, locPath);
				}
				
			}
//			prestmt.executeBatch();
			prestmt.clearBatch();
            logger.info("保存server数据"+"热门"+"完成:"+index); 
    		MysqlLocalConnect.insert(loaclmap, "热门");
        }catch(SQLException e){
        	logger.error("insertHot,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertHot,Exception:"+e.getMessage());
        }finally{
            close();
            MysqlLocalConnect.close();
        }
    
    }
    
    /**
     * 保存趣图（文本+图片）
     * @param map
     */
    public static void insertImageLoco(Map<String,String> map) {
    	String Y = DateUtil.formatString(new Date(), "yyyy");
    	String M = DateUtil.formatString(new Date(), "MM");
    	String D = DateUtil.formatString(new Date(), "dd");
    	String ymd = Y+"/"+M+"/"+D;
    	getConnect();
    	MysqlLocalConnect.getConnect();
        try{
            String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES ('趣图', ?, ?, 0, 0, 0, ?, ?, ?, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[map.size()];
    		
    		for (int j = 0; j < map.size(); j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		
    		logger.info("趣图ids总长度："+ids.length);
    		int type = 1;
    		int index = 0;
    		Date date = new Date();
    		Map<String, String> loaclmap = new HashMap<>();
			for (String str : map.keySet()) {
				String path = map.get(str).trim();
				String content = str.trim();
				if(MysqlLocalConnect.query(content, path)) {
					String locContent = content.trim();
					String locPath = path.trim();
					if (!"".equals(path.trim())) {
						path = Const.reSymbol+Const.jokePre+ymd+Const.o+path;
						type = 2;		
						content = "&lt;p&gt;" + content + "&lt;br/&gt;" + "&lt;img src=&quot;" + path
								+ "&quot; alt=&quot;&quot; val=&quot;" + path + "&quot;/&gt;" + "&lt;/p&gt;";
					}else {
						content = "&lt;p&gt;"+content+"&lt;/p&gt;";
						type = 1;
					}
					
					prestmt.setString(1,path.trim());//路径
	            	prestmt.setString(2,content.trim());//内容
					prestmt.setLong(3, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
					prestmt.setLong(4, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
	            	prestmt.setLong(5,type);//类型
	            	prestmt.setLong(6,ids[index]);//用户Id 
	            	prestmt.setLong(7, new Random().nextInt(950) + 50);//赞
	            	prestmt.setLong(8, new Random().nextInt(95) + 5);//踩
					prestmt.addBatch();
					index++;
					loaclmap.put(locContent, locPath);
				}
				
			}
			
//			prestmt.executeBatch();
			prestmt.clearBatch();
            logger.info("保存server数据"+"趣图"+"完成:"+index); 
    		MysqlLocalConnect.insert(loaclmap, "趣图");
        }catch(SQLException e){
        	logger.error("insertImage,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertImage,Exception:"+e.getMessage());
        }finally{
            close();
            MysqlLocalConnect.close();
        }
    
    }
    
    /**
     * 保存视频（文本+视频mp4）
     * @param map
     */
	public static void insertVideoLoco(Map<String, String> map) {
    	String Y = DateUtil.formatString(new Date(), "yyyy");
    	String M = DateUtil.formatString(new Date(), "MM");
    	String D = DateUtil.formatString(new Date(), "dd");
    	String ymd = Y+"/"+M+"/"+D;
    	getConnect();
    	MysqlLocalConnect.getConnect();
        try{
            String sql = "INSERT INTO yi_user_joke " + 
            		" (title,image,content,is_package,package_fee,package_user_id,created_time,audit_time,type,status,user_id,audit_num,good_num,bad_num,review_num,share_num,award_num,tags_id,god_reply,reason,commend) " + 
            		" VALUES (?, ?, ?, 0, 0, 0, ?, ?, ?, 1, ?, 0, ?, ?, 0, 0, 0, '', 0, '', 0)";
            prestmt = conn.prepareStatement(sql);
            
            int userId = 8;
    		Integer[] ids = new Integer[map.size()];
    		
    		for (int j = 0; j < map.size(); j++) {
    			userId= new Random().nextInt(100) + 8;
    			ids[j] = userId;
    		}
    		
    		logger.info("视频ids总长度："+ids.length);
    		int type = 1;
    		int index = 0;
    		Date date = new Date();
    		Map<String, String> loaclVodeoMap = new HashMap<>();
    		Map<String, String> loaclVodeoImageMap = new HashMap<>();
			for (String str : map.keySet()) {
				String pathTwo = map.get(str).trim();
				String[] pathTwos = pathTwo.split("\\|\\|");
				if (pathTwos.length<2) continue;
				String videoImagePath = pathTwos[0];
				String videoPath = pathTwos[1];
				String content = str.trim();
				if(MysqlLocalConnect.query(content, videoPath)) {
					String locContent = content.trim();
					String locVideoPath = videoPath.trim();
					String locImagePath = videoImagePath.trim();
					if (StringUtil.isNotEmpty(videoPath)) {
						videoImagePath = Const.reSymbol+Const.jokePre+ymd+Const.o+videoImagePath;
						videoPath = Const.reSymbol+Const.jokePre+ymd+Const.o+videoPath;
						type = 4;	//修改类型和显示格式  将对应属性存到对应字段里
						videoImagePath = "&lt;img src=&quot;" + videoImagePath
								+ "&quot; alt=&quot;&quot; val=&quot;" + videoImagePath + "&quot;/&gt;";
					
						prestmt.setString(1,content.trim());//标题  是显示内容
						prestmt.setString(2,videoImagePath.trim());//图片路径标签
						prestmt.setString(3,videoPath.trim());//视频URL
						prestmt.setLong(4, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
						prestmt.setLong(5, DateUtil.getMills(date, DateUtil.YMDFHMS)/1000);
						prestmt.setLong(6,type);//类型
						prestmt.setLong(7,ids[index]);//用户Id 
						prestmt.setLong(8, new Random().nextInt(950) + 50);//赞
						prestmt.setLong(9, new Random().nextInt(95) + 5);//踩
						prestmt.addBatch();
						loaclVodeoMap.put(locContent, locVideoPath);
						loaclVodeoImageMap.put(locContent, locImagePath);
					}
				}
				index++;
			}
			
//			prestmt.executeBatch();
			prestmt.clearBatch();
            logger.info("保存server数据"+"视频"+"完成:"+index); 
    		MysqlLocalConnect.insert(loaclVodeoMap, "视频");
    		MysqlLocalConnect.insert(loaclVodeoImageMap, "视频快照");
        }catch(SQLException e){
        	logger.error("insertImage,SQLException:"+e.getMessage());
        }catch(Exception e){
        	logger.error("insertImage,Exception:"+e.getMessage());
        }finally{
            close();
            MysqlLocalConnect.close();
        }
    
    } 
    
	
    public void query() {
    	getConnect();
        try{
            String sql;
            sql = " ";
            
            rs = stmt.executeQuery(sql);
            
            while(rs.next()){
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
          
        }catch(SQLException e){
        	logger.info(""+e.getMessage());
        }catch(Exception e){
        	logger.info(""+e.getMessage());
        }finally{
        	close();
        }
    }

	public static void main(String[] args) {
		Integer[] acd = new Integer[500];
		int userId = 8;
		
		for (int j = 0; j < 500; j++) {
			userId= new Random().nextInt(100) + 8;
			acd[j] = userId;
		}
		
		for (int i = 0; i < acd.length; i++) {
			logger.info(String.valueOf(acd[i]));
		}
		logger.info("总长度："+acd.length);
	}

    
}
